package net.wicp.tams.commons.binlog.replication.down;

import java.io.IOException;

import org.apache.tapestry5.json.JSONObject;

import net.wicp.tams.commons.apiext.ByteUtil;
import net.wicp.tams.commons.apiext.ByteUtil.AssitRead;
import net.wicp.tams.commons.binlog.bean.PacketBean;
import net.wicp.tams.commons.binlog.constant.ColumnType;
import net.wicp.tams.commons.binlog.replication.AbsPacketRead;

public class RsFieldPacket extends AbsPacketRead {
	private static final long serialVersionUID = 1L;

	public RsFieldPacket(PacketBean packetBean) {
		super(packetBean);
	}

	//
	private String catalog;
	private String db;
	private String table;
	private String orginalTable;
	private String column;
	private String originalColumn;
	private int fixed12;
	private int charset;
	private long fieldLength;
	private ColumnType fieldType;
	private int fieldOptions;
	private int decimalPrecision;
	private int reserved;
	private String defaultValue; // Optional

	@Override
	public JSONObject getBody() {
		JSONObject retobj = new JSONObject("catalog", catalog, "db", db, "table", table, "orginalTable", orginalTable,
				"column", column, "originalColumn", originalColumn, "fixed12", fixed12, "charset", charset,
				"fieldLength", fieldLength, "fieldType", fieldType.name(), "fieldOptions", fieldOptions,
				"decimalPrecision", decimalPrecision, "reserved", reserved, "defaultValue", defaultValue);
		return retobj;
	}

	@Override
	protected void readBody(AssitRead reader) throws IOException {
		catalog = reader.readStringUnsignedLong();
		db = reader.readStringUnsignedLong();
		table = reader.readStringUnsignedLong();
		orginalTable = reader.readStringUnsignedLong();
		column = reader.readStringUnsignedLong();
		originalColumn = reader.readStringUnsignedLong();
		fixed12 = ByteUtil.readIntL(reader.readBytes(1));
		charset = ByteUtil.readIntL(reader.readBytes(2));
		fieldLength = ByteUtil.readLongL(reader.readBytes(4));
		fieldType = ColumnType.get(reader.readByte());
		fieldOptions = ByteUtil.readIntL(reader.readBytes(2));
		decimalPrecision = ByteUtil.readIntL(reader.readBytes(1));
		reserved = ByteUtil.readIntL(reader.readBytes(2));
		if (reader.hasMore()) {
			defaultValue = reader.readStringUnsignedLong();
		}
	}

	public String getCatalog() {
		return catalog;
	}

	public String getDb() {
		return db;
	}

	public String getTable() {
		return table;
	}

	public String getOrginalTable() {
		return orginalTable;
	}

	public String getColumn() {
		return column;
	}

	public String getOriginalColumn() {
		return originalColumn;
	}

	public int getFixed12() {
		return fixed12;
	}

	public int getCharset() {
		return charset;
	}

	public long getFieldLength() {
		return fieldLength;
	}

	public ColumnType getFieldType() {
		return fieldType;
	}

	public int getFieldOptions() {
		return fieldOptions;
	}

	public int getDecimalPrecision() {
		return decimalPrecision;
	}

	public int getReserved() {
		return reserved;
	}

	public String getDefaultValue() {
		return defaultValue;
	}
}
